package main

import (
	"gin-jwt-test/middleware"
	"gin-jwt-test/model"
	"github.com/gin-gonic/gin"
	"log"
)

func main() {
	r := gin.Default()
	auth := middleware.Auth()
	initAuthErr := auth.MiddlewareInit()
	if initAuthErr != nil {
		log.Fatalln(initAuthErr)
	}

	r.GET("/register", func(context *gin.Context) {
		username := context.DefaultQuery("username", "")
		password := context.DefaultQuery("password", "")
		u := model.User{}
		res, err := u.Create(username, password)
		if err != nil {
			log.Fatalln(err)
		}
		context.JSON(200, res)
	})

	r.GET("/login", auth.LoginHandler)
	r.GET("/", func(context *gin.Context) {
		context.JSON(200, gin.H{
			"Hello": "World",
		})
	})
	authRoute := r.Group("/auth").Use(auth.MiddlewareFunc())
	{
		authRoute.GET("/hello", func(context *gin.Context) {
			context.JSON(200, gin.H{
				"hello": "world",
			})
		})
	}
	log.Fatal(r.Run(":8080"))
}
